class PubSub{
    constructor(){
        this.sub = {}
    }
    subscribe(type,handler){//订阅，往sub对象的属性中添加handler
        if(!this.sub[type]){//不存在就初始化[]
            this.sub[type]=[]
        }
        this.sub[type].push(handler)//添加handler
    }
    publish(type,params){
        if(!this.sub[type]){
            return;
        }
        this.sub[type].forEach(handler=>{
            handler(params)
        })
    }
}
let sub1=function(params){
    console.log('订阅1',params)
}
let sub2=function(params){
    console.log('订阅2',params)
}
let sub3=function(params){
    console.log('订阅3',params)
}
let pubSub = new PubSub();
pubSub.subscribe('吃',sub1)
pubSub.subscribe('喝',sub2)
pubSub.subscribe('喝',sub3)

pubSub.publish('吃','狗屎')
pubSub.publish('喝','尿')